package zen2d;


import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

public class GraphicsContext {

    private Graphics2D targetGraphics;

    private static Map<BufferedImage, Image> scaleCache;

    public GraphicsContext(BufferedImage source) {
        targetGraphics = source.createGraphics();
        //消除文字锯齿
        targetGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //消除画图锯齿
        targetGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        scaleCache = new HashMap<>();
    }

    public void setColor(Color color) {
        targetGraphics.setColor(color);
    }

    public void drawRect(int x, int y, int width, int height, boolean fill) {
        if (fill) {
            targetGraphics.fillRect(x, y, width, height);
        } else {
            targetGraphics.drawRect(x, y, width, height);
        }
    }

    public void clearRect(int x, int y, int width, int height) {
        targetGraphics.clearRect(x, y, width, height);
    }

    public void drawOval(int x, int y, int width, int height, boolean fill) {
        if (fill) {
            targetGraphics.fillOval(x, y, width, height);
        } else {
            targetGraphics.drawOval(x, y, width, height);
        }
    }

    public void drawImage(BufferedImage image, int x, int y) {
        targetGraphics.drawImage(image, x, y, null);
    }

    public void drawImage(BufferedImage image, int x, int y, int width, int height) {
        if (Zen2d.isHighQuality()) {
            Image scaleImg = scaleCache.get(image);
            if (scaleImg == null) {
                scaleImg = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
                scaleCache.put(image, scaleImg);
            }
//            BufferedImage newImage = Scalr.resize(image, Scalr.Method.ULTRA_QUALITY,
//                    width,height);//
            targetGraphics.drawImage(scaleImg, x, y, null);
        } else {
            targetGraphics.drawImage(image, x, y, width, height, null);
        }
    }

    public void drawString(String str, int x, int y, int size) {

        Map<TextAttribute, Object> fontAttr = new HashMap<>();
        fontAttr.put(TextAttribute.FAMILY, Font.MONOSPACED);
        fontAttr.put(TextAttribute.SIZE, size);
        fontAttr.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
        Font font = new Font(fontAttr);

        targetGraphics.setFont(font);
        targetGraphics.drawString(str, x, y);
    }

}
